package com.itwn.portal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwn.portal.model.entity.Address;
import com.itwn.portal.model.entity.China;
import com.itwn.portal.model.vo.AddressRequest;
import com.itwn.portal.model.vo.AddressVo;
import com.itwn.portal.service.AddressService;
import com.itwn.portal.mapper.AddressMapper;
import com.itwn.portal.service.ChinaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author 61073
* @description 针对表【t_address】的数据库操作Service实现
* @createDate 2024-12-18 10:03:40
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{
    
    @Autowired
    private ChinaService chinaService;
    
    @Override
    public List<AddressVo> findAddress(long userId) {
        // 根据用户id查询用户所有的地址
        List<Address> addressList = this.list(
                Wrappers.lambdaQuery(Address.class).eq(Address::getUserId, userId));
        // 将地址列表转换为地址Vo列表
        List<AddressVo> addressVoList = addressList.stream().map(address -> {
            AddressVo addressVo = new AddressVo();
            addressVo.setId(address.getId());
            addressVo.setLabel(address.getProvince()+address.getCity()+address.getArea()+address.getDetailAddress());
            addressVo.setIsDefault(address.getIsDefault());
            return addressVo;
        }).collect(Collectors.toList());
        return addressVoList;
    }

    @Transactional
    @Override
    public void saveAddress(String userId, AddressRequest request) {
        // 查询
        China province = chinaService.findById(request.getProvince());
        China city = chinaService.findById(request.getCity());

        // 数据问题： request.getDistrict() 有可能为空，为空不要查询
        //if (request.getDistrict() != null) {
        //    China district = chinaService.getById(request.getDistrict());
        //}
        China district = Optional.ofNullable(request.getDistrict())
                .map(chinaService::findById)
                .orElse(null);


        // 封装地址对象
        Address address = new Address();
        address.setProvince(province.getName());
        address.setCity(city.getName());
        address.setArea(district != null ? district.getName() : "");

        address.setDetailAddress(request.getDetailAddress());
        address.setTelephone(request.getTel());
        address.setReceiver(request.getReceiver());
        address.setStatus("1");
        address.setUserId(Long.parseLong(userId));
        address.setIsDefault(request.getIsDefault());

        // 判断地址如果是默认地址（1）， 就需要根据userId修改用户的其他所有地址的isDefault字段为0
        if (request.getIsDefault() == 1) {
            this.update(
                    Wrappers.lambdaUpdate(Address.class)
                            .set(Address::getIsDefault, 0)
                            .eq(Address::getUserId, userId));
        }

        baseMapper.insert(address);
    }
}




